package com.rjyb.student_management_platform.controller;

import com.rjyb.student_management_platform.pojo.Class;
import com.rjyb.student_management_platform.pojo.*;
import com.rjyb.student_management_platform.service.AnswerService;
import com.rjyb.student_management_platform.unit.common.DateTime;
import com.rjyb.student_management_platform.unit.common.Pager;
import com.rjyb.student_management_platform.unit.load.FileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("answer")
public class AnswerController {
    @Autowired
    AnswerService as;
    DateTime dateTime = new DateTime();
    //老师填写班级，查询班级
    @RequestMapping("queryClass")
    public String queryClass(Model model,HttpSession session){
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        List<Class> list = as.classid(teacher,session,model);
        Booksubject list1 = as.selBooksubject(session);
        model.addAttribute("list1",list1);
        model.addAttribute("list",list);
        return "teacher/lyear_pages_add_doc";
    }


    //老师上传作业
    @RequestMapping("homework")
    public String homework(MultipartFile file1,MultipartFile file2,MultipartFile file3,MultipartFile file4, HttpSession session,Jobtable jobtable){
        Teacher teacher = (Teacher) session.getAttribute("teacher");

        jobtable.setJobData(dateTime.nowTime().substring(0,11));

        jobtable.setTid(teacher.getTid());
        as.addJobtable(jobtable,teacher,session,file1,file2,file3,file4);
        return "redirect:queryClass";
    }


    //老师查作业
    @RequestMapping("selectTHomeWork")
    public  String selectTHomeWork(Model model ,HttpSession session){
        Teacher teacher = (Teacher) session.getAttribute("teacher");
      Integer ttid=(int)teacher.getTid();
       List<HashMap> list= as.selectSumHomeWork(ttid);
       model.addAttribute("list",list);
        return "teacher/Class_Teacher_HomeWork";
    }

    @RequestMapping("selectAnswer")
    public String selectAnswer(Model model, Pager pager,HttpSession session,Integer jjid){

        pager.setTotalRow(as.selcountanswer(jjid));
        List<Map> list=as.answerlist(pager,jjid);
        for (Map map : list) {
            System.out.println(map.toString());
        }
        model.addAttribute("list",list);
        model.addAttribute("jjid",jjid);

        return "teacher/lyear_pages_doc";//标签改动
    }
//有改动
    @RequestMapping("toupdate")
    public String toupdate(Integer homeWorkid, HttpSession session,Model model,Integer ssid,Integer jjid){
        List<HashMap> list= as.selectimg(homeWorkid,ssid);
        for (Map map : list) {
            System.out.println(map.toString());
        }
        model.addAttribute("list",list);
        session.setAttribute("homeWorkid",homeWorkid);
        session.setAttribute("ssid",ssid);
        model.addAttribute("jjid",jjid);
        return "teacher/view";
    }

    @RequestMapping("updateWork")
    public  String updateWork(Integer correct,Integer remedy,HttpSession session,Integer jjid,Model model,String mistake){
        Integer ssid= (Integer) session.getAttribute("ssid");
        System.out.println(mistake);

        as.updateWork(correct,remedy,(Integer) session.getAttribute("homeWorkid"),ssid,mistake);
        model.addAttribute("jjid",jjid);
        System.out.println(jjid);
        return "redirect:selectAnswer?jjid="+jjid;
    }
//改动结束线--------------------------------

    //查询学生作业
    @RequestMapping("toSelectWork")
    public String toSelectWork(HttpSession session,Model model){
       Student student=(Student) session.getAttribute("student");
       Long sid = student.getSid();
        List<HashMap> list= as.selectHomeWork(sid);
        List<HashMap> list1=new ArrayList<HashMap>();
        for (HashMap hashMap : list) {
            Integer jjid=(int)hashMap.get("jid");
            Integer ssid=(int)hashMap.get("sid");
            if (as.ifHomeWork1(jjid,ssid)==null){
                //ifHomeWork1判断作业做没做 null=没做
                list1.add(hashMap);
            }

        }

        model.addAttribute("list",list1);
        return "student/stuHomeWork";
    }
    //上传答案
    @RequestMapping("/toupAnswer")
    public String   toupAnswer(int jid,HttpSession session,Model model){
       List<HashMap> list= as.selectTeacherName(jid);
        session.setAttribute("jid",jid);//传作业id
        model.addAttribute("list",list);
        return "student/Homework";
    }
    //上传答案
    @RequestMapping("upAnswer")
    public String upAnswer(MultipartFile file1,MultipartFile file2,MultipartFile file3,MultipartFile file4, HttpSession session,String answer){
        AnswerSheet answerSheet=new AnswerSheet();
        //  session.setAttribute("stuId")获取学生id
        Student stu= (Student) session.getAttribute("student");
         int stuId=  (int)stu.getSid();
        Integer jid= (Integer) session.getAttribute("jid");//获取作业id
        answerSheet.setJid(jid);
        //给答案做非空判断
        String qq=null;
        if (answer!=null){
            answerSheet.setAnswer(answer);
        }else {answerSheet.setAnswer(qq);
        }
        //给answerSheet加值
        answerSheet.setAnswerTime(dateTime.nowTime().substring(0,11));
        answerSheet.setSid(stuId);
        answerSheet.setImgsrc(FileUpload.upload(file1,session,"file1"));
        answerSheet.setImgsrc1(FileUpload.upload(file2,session,"file2"));
        answerSheet.setImgsrc2(FileUpload.upload(file3,session,"file3"));
        answerSheet.setImgsrc3(FileUpload.upload(file4,session,"file4"));
        if(answerSheet.getImgsrc1()==null){
            answerSheet.setImgsrc1("undefined");
        }
        if(answerSheet.getImgsrc2()==null){
            answerSheet.setImgsrc2("undefined");
        }
        if(answerSheet.getImgsrc3()==null){
            answerSheet.setImgsrc3("undefined");
        }
        if(answerSheet.getImgsrc()==null){
            answerSheet.setImgsrc("undefined");
        }
        as.addAnswerSheet(answerSheet);
        return "redirect:toSelectWork";
    }

    //学生查询是否批改
    @RequestMapping("ifCorrect")
    public String ifCorrect(Model model,HttpSession session){
        List<HashMap> list1=new ArrayList<HashMap>();
        //  session.setAttribute("stuId")获取学生id
        Student stu= (Student) session.getAttribute("student");
        Long stuId=  stu.getSid();

        List<HashMap> list= as.selectHomeWork(stuId);
        //as.selectHomeWork查作业

        for (HashMap hashMap : list) {
            Integer jjid=(int)hashMap.get("jid");
            Integer ssid=(int)hashMap.get("sid");
            if (as.ifHomeWork1(jjid,ssid)!=null){
                //ifHomeWork1判断作业做没做 null=没做
            HashMap map =as.ifHomeWork(jjid,ssid);
                //ifHomeWork查询作业改没改
                list1.add(map);
            }

        }


        model.addAttribute("list",list1);
        return "student/stuNoHomeWork";
    }

    //学生拿没过的作业
    @RequestMapping("TakeHomework")
    public String TakeHomework(String aid,HttpSession session,Model model){
        if(aid!=null&&!aid.equals("")){
            List<Map> list = as.TakeHomework(aid,session,model);
            model.addAttribute("list",list);
            System.out.println(list);
            return "student/Homework_student";
        } else {
            List<Map> list = as.TakeHomework(aid,session,model);
            model.addAttribute("list",list);
            return "student/Returned_jobs";
        }
    }

    //学生修改完提交作业
    @RequestMapping("Submitthejob")
    public String Submitthejob(MultipartFile file1,MultipartFile file2,MultipartFile file3,MultipartFile file4, HttpSession session,AnswerSheet answerSheet,Model model){
        DateTime dateTime = new DateTime();
        answerSheet.setAnswerTime(dateTime.nowTime().substring(0,11));
        Student answerSheet1 = (Student) session.getAttribute("student");
        answerSheet.setSid(answerSheet1.getSid());
        System.out.println(answerSheet);
        as.UpdateAnswerSheet(session,answerSheet,file1,file2,file3,file4,model);

        return "redirect:TakeHomework";
    }

}
